package com.bw.user.controller;


import cn.hutool.core.util.ObjectUtil;
import com.bw.core.result.ResponseResult;
import com.bw.user.service.TbIdentityAuthService;
import com.bw.user.task.IdentityAuthTask;
import com.bw.user.task.LiveAuthTask;
import com.bw.user.task.UploadTask;
import com.bw.user.vo.IdentityAuthVo;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Base64;
import java.util.Map;
import java.util.concurrent.*;

/**
 * <p>
 * 身份认证表&信息 前端控制器
 * </p>
 *
 * @author lv
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/tb-identity-auth")
public class TbIdentityAuthController {

    @Autowired
    private TbIdentityAuthService identityAuthService;

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private Redisson redisson;


    private ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(32, 52, 2, TimeUnit.SECONDS, new LinkedBlockingQueue<>(200));

    /**
     * @param IdCardFront
     * @return 扫描身份证
     */

    @PostMapping("/scanidcard")
    public ResponseResult scanidcard(MultipartFile IdCardFront) {
        if (ObjectUtil.isNull(IdCardFront)) {
            return ResponseResult.error("为空");
        }
        return identityAuthService.scanidcard(IdCardFront);
    }

    /* *//**
     *
     * @param identityAuthVo
     * @param realname
     * @return
     * 实名认证
     *//*
    @PostMapping("/realname")
    public ResponseResult certification(@RequestBody IdentityAuthVo identityAuthVo, MultipartFile realname)
    {
        if (ObjectUtil.isNull(realname)){
            return ResponseResult.error("为空");
        }
        return identityAuthService.certification(identityAuthVo,realname);
    }
*/
    /*   */

    /**
     * @param
     * @return 活体认证
     *//*
    @PostMapping("/livingauthen")
    public ResponseResult livingauthen(MultipartFile livePic)
    {
        if (ObjectUtil.isNull(livePic)){
            return ResponseResult.error("为空");
        }
        return identityAuthService.livingauthen(livePic);
    }*/
    @PostMapping("/identityAuth")
    public ResponseResult identityAuth(IdentityAuthVo identityAuthVo) {
        /* String token = httpServletRequest.getHeader("token");
         */
        if (ObjectUtil.isNull(identityAuthVo.getIdCardFront())) {
            return ResponseResult.error("身份证正面为空");
        }

        if (ObjectUtil.isNull(identityAuthVo.getIdCardBank())) {
            return ResponseResult.error("身份证反面为空");
        }

        if (ObjectUtil.isNull(identityAuthVo.getLivePic())) {
            return ResponseResult.error("活体认证为空");
        }


        RLock lockName = redisson.getLock("lockName");
        try {
            // 1. 最常见的使用方法
            //lock.lock();
            // 2. 支持过期解锁功能,10秒钟以后自动解锁, 无需调用unlock方法手动解锁
            //lock.lock(10, TimeUnit.SECONDS);
            // 3. 尝试加锁，最多等待2秒，上锁以后8秒自动解锁
            boolean res = lockName.tryLock(2, 8, TimeUnit.SECONDS);
            if (res) { //成功
                //处理业务
                long startTime = System.currentTimeMillis();
                String identityAuth = Base64.getEncoder().encodeToString(identityAuthVo.getIdCardFront().getBytes());//构建任务
                IdentityAuthTask identityAuthTask = new IdentityAuthTask(identityAuthVo.getName(), identityAuthVo.getCardId(), identityAuthVo.getIdCardFront(), identityAuthService);//提交任务
                Future<Boolean> identityAuthTaskFuture = threadPoolExecutor.submit(identityAuthTask);

                String livepic = Base64.getEncoder().encodeToString(identityAuthVo.getLivePic().getBytes());//构建任务
                LiveAuthTask liveAuthTask = new LiveAuthTask(identityAuthVo.getLivePic(), identityAuthService);//提交任务
                Future<Boolean> liveAuthTaskFuture = threadPoolExecutor.submit(liveAuthTask);

                UploadTask uploadTask = new UploadTask(identityAuthVo.getIdCardFront(), identityAuthVo.getLivePic(), identityAuthVo.getIdCardBank(), identityAuthService);//提交任务
                Future<Map> uploadTaskFuture = threadPoolExecutor.submit(uploadTask);

                Boolean s1 = identityAuthTaskFuture.get();
                Boolean s2 = liveAuthTaskFuture.get();
                Map<String, String> map = uploadTaskFuture.get();

                return identityAuthService.iditiy(identityAuthVo, map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("认证失败");
        } finally {
            //释放锁
            lockName.unlock();
        }

        return null;
    }
}

